/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef HKS_ACCESS_CONTROL_TEST_COMMON_H
#define HKS_ACCESS_CONTROL_TEST_COMMON_H

#include "hks_three_stage_test_common.h"
#include "hks_core_hal_api.h"

#include <vector>
#include <string>

namespace Unittest::HksAccessControlPartTest {
#define SHA256_SIGN_LEN 32
#define SHA256_KEY_LEN 32
#define AUTH_TOKEN_LEN sizeof(HksUserAuthToken)
#define AUTH_TOKEN_DATA_LEN (AUTH_TOKEN_LEN - SHA256_SIGN_LEN)
#define TOKEN_CHALLENGE_LEN 32
#define HKS_DEFAULT_USER_AT_KEY "huks_default_user_auth_token_key"

static const std::string g_inData = "Hks_Authtoken_Test_0000000000000000000000000000000000000000000000000000000000000"
                                    "00000000000000000000000000000000000000000000000000000000000000000000000000000000"
                                    "0000000000000000000000000000000000000000000000000000000000000000000000000_string";
static const uint32_t IV_SIZE = 16;
const uint32_t KEY_PARAMSET_SIZE = 1024;
const uint32_t HMAC_COMMON_SIZE = 256;
const uint32_t DATA_COMMON_SIZE = 1024;
const uint32_t RSA_COMMON_SIZE = 1024;
const uint32_t ECDH_COMMON_SIZE = 1024;
const uint32_t DERIVE_KEY_SIZE_32 = 32;
const uint32_t DERIVE_ITERATION = 1000;
const uint32_t DERIVE_COMMON_SIZE = 2048;

static uint8_t IV[IV_SIZE] = {0};
static uint8_t g_saltdata2[16] = {0};

struct TestAccessCaseParams {
    std::vector<HksParam> genParams;
    std::vector<HksParam> initParams;
    HksErrorCode initResult = HksErrorCode::HKS_SUCCESS;
};

int32_t AddAuthtokenUpdateFinish(struct HksBlob *handle, struct HksParamSet *initParamSet, uint32_t posNum);
int32_t CheckAccessCipherTest(const TestAccessCaseParams &testCaseParams, 
uint64_t secureUid, uint64_t enrolledId, uint64_t time);

int32_t CheckAccessHmacTest(const TestAccessCaseParams &testCaseParams, 
uint64_t secureUid, uint64_t enrolledId, uint64_t time);

int32_t CheckAccessAgreeTest(const TestAccessCaseParams &testCaseParams, const struct HksParamSet *finishParamSet, 
uint64_t secureUid, uint64_t enrolledId, uint64_t time);

int32_t CheckAccessDeriveTest(const TestAccessCaseParams &testCaseParams, const struct HksParamSet *finishParamSet, 
uint64_t secureUid, uint64_t enrolledId, uint64_t time);

#ifdef __cplusplus
extern "C" {
#endif

int32_t AuthTokenImportKey();
//If time = 0, function will set the correct time automatically
int32_t AuthTokenSign(struct HksBlob *challenge, uint64_t secureUid, uint64_t enrolledId, uint64_t time, std::vector<uint8_t>& token);
int32_t HksBuildAuthtoken(const struct HksParamSet *initParamSet, uint8_t *authChallenge, uint64_t secureUid, 
    uint64_t enrolledId, uint64_t time);

#ifdef __cplusplus
}
#endif
}
#endif // HKS_THREE_STAGE_TEST_COMMON_H